package com.lebo.portal.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lebo.portal.constant.Constants;
import com.lebo.portal.controller.base.BaseController;
import com.lebo.portal.model.LoginUser;
import com.lebo.portal.service.ActivityService;
import com.lebo.portal.service.BlogService;
import com.lebo.portal.view.PageInfo;
import com.lebo.portal.view.PageQuery;
import com.lebo.portal.view.PageResult;
import com.lebo.web.domain.Activity;
import com.lebo.web.domain.Blog;

@Controller
public class PresentationController extends BaseController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String root(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user) {
        return "redirect:home";
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView index(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user) {
        mv.setViewName("index");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }

    @RequestMapping(value = "/function", method = RequestMethod.GET)
    public ModelAndView function(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user) {
        mv.setViewName("function");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }

    @RequestMapping(value = "/cases", method = RequestMethod.GET)
    public ModelAndView cases(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user,
            PageQuery<Activity> pageQuery, Integer categoryId, PageInfo pageInfo) {
        if(null != categoryId) {
            Activity activity = new Activity();
            activity.setCategoryId(categoryId);
            pageQuery.setFilter(activity);
        }
        pageQuery.setPageInfo(pageInfo);
        PageResult<List<Activity>> pageResult = activityService.pagingActivities(pageQuery);

        mv.addObject("pageResult", pageResult);
        mv.addObject("categoryId", categoryId);

        mv.setViewName("cases");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }

    @RequestMapping(value = "/blog", method = RequestMethod.GET)
    public ModelAndView blog(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user,
            PageQuery<Blog> pageQuery, Integer categoryId, PageInfo pageInfo) {
        if(null != categoryId) {
            Blog blog = new Blog();
            blog.setCategoryId(categoryId);
            pageQuery.setFilter(blog);
        }
        pageQuery.setPageInfo(pageInfo);
        PageResult<List<Blog>> pageResult = blogService.pagingBlogs(pageQuery);

        mv.addObject("pageResult", pageResult);
        mv.addObject("categoryId", categoryId);

        mv.setViewName("blog");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public ModelAndView about(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user) {
        mv.setViewName("about");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }

    @RequestMapping(value = "/help", method = RequestMethod.GET)
    public ModelAndView help(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user) {
        mv.setViewName("help");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(ModelAndView mv, @ModelAttribute(Constants.SESSION_ACCOUNT_LABLE) LoginUser user) {
        mv.setViewName("login");
        mv.addObject(Constants.SESSION_ACCOUNT_LABLE, user);
        return mv;
    }
}